package com.hotai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hotai.comment.dtos.PageResponseResult;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.md5.MD5Utils;
import com.hotai.comment.pojo.Ban;
import com.hotai.comment.pojo.BanRenew;
import com.hotai.comment.pojo.Courseware;
import com.hotai.comment.pojo.Institution;
import com.hotai.mapper.BanRenewMapper;
import com.hotai.mapper.InstitutionMapper;
import com.hotai.mapper.SheGenMapper;
import com.hotai.service.InstitutionSerivice;
import com.hotai.service.SheGenService;
import com.hotai.vo.ban.*;
import com.hotai.vo.courseware.CoursewareSave;
import com.hotai.vo.courseware.CoursewareSaveFindBy;
import com.hotai.vo.equipment.EquipmentPage;
import com.hotai.vo.tupian.Pian;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

@Service
public class SheGenServiceImpl extends ServiceImpl<SheGenMapper, Ban> implements SheGenService {
    @Autowired
    private SheGenMapper sheGenMapper;

    @Autowired
    private BanRenewMapper banRenewMapper;

    private static final String Salt = "MHCDEKLDE321";

    @Override
    public ResponseResult genxin(String name) {
//         Ban  ban=sheGenMapper.newest(name);
        Ban ban = sheGenMapper.genxin();
        List<BanRenew> banRenews = banRenewMapper.selectList(Wrappers.<BanRenew>lambdaQuery().eq(BanRenew::getBanid, ban.getId()));
        int i = 0;
        for (BanRenew banRenew : banRenews) {
            if (StringUtils.isNotBlank(banRenew.getEqid())) {
                String[] split = banRenew.getEqid().split(",");
                for (String s : split) {
                    if (s.equals(name)) {
                        ban.setStatus(banRenew.getStatus());
                        ban.setContent(banRenew.getContent());
                        i++;
                        break;
                    }
                }
            }

        }
        if (i == 0) {
            return ResponseResult.okResult(null);
        }
        return ResponseResult.okResult(ban);
    }

    @Override
    public ResponseResult saveban(BanSave ban) {
        if (ban.getId() == null) {
            List<Ban> bans = sheGenMapper.selectList(Wrappers.<Ban>lambdaQuery().eq(Ban::getName, ban.getName()));
            if (bans.size() > 0) {
                return ResponseResult.errorResult(530, "版本重复");
            }
            Ban ban1 = new Ban();
//             ban.setName(ban.getName()+"V");
            BeanUtils.copyProperties(ban, ban1);
            sheGenMapper.insert(ban1);
            //强制
            Ban ban2 = sheGenMapper.selectOne(Wrappers.<Ban>lambdaQuery().eq(Ban::getName, ban.getName()));
            BanRenew banRenew = new BanRenew();

            BanQiang banQiang1 = ban.getBanQiang1();
            banRenew.setBanid(ban2.getId());
            banRenew.setStatus(1);
            String tname = sname(banQiang1.getName());
            banRenew.setEqid(tname);
            banRenew.setContent(banQiang1.getContent());
            banRenewMapper.insert(banRenew);
            //非强制
            BanQiang banQiang2 = ban.getBanQiang2();
            banRenew.setBanid(ban2.getId());
            banQiang2.setStatus(2);
            banRenew.setStatus(banQiang2.getStatus());
            String tname2 = sname(banQiang2.getName());
            banRenew.setEqid(tname2);
            banRenew.setContent(banQiang2.getContent());
            banRenewMapper.insert(banRenew);
            //不跟新
            BanQiang banQiang3 = ban.getBanQiang3();
            banRenew.setBanid(ban2.getId());
            banQiang3.setStatus(3);
            banRenew.setStatus(banQiang3.getStatus());
            String tname3 = sname(banQiang3.getName());
            banRenew.setEqid(tname3);
            banRenew.setContent(banQiang3.getContent());
            banRenewMapper.insert(banRenew);
        } else {
            Ban ban1 = new Ban();
            BeanUtils.copyProperties(ban, ban1);
            sheGenMapper.updateById(ban1);
            BanQiang banQiang1 = ban.getBanQiang1();
            if (banQiang1 != null) {
                BanRenew banRenew = new BanRenew();
                banRenew.setId(banQiang1.getId());
                banRenew.setBanid(ban.getId());
                banRenew.setStatus(banQiang1.getStatus());
                String tname = sname(banQiang1.getName());
                banRenew.setEqid(tname);
                banRenew.setContent(banQiang1.getContent());
                banRenewMapper.updateById(banRenew);
            }
            BanQiang banQiang2 = ban.getBanQiang2();
            if (banQiang2 != null) {
                BanRenew banRenew = new BanRenew();
                banRenew.setBanid(ban.getId());
                banRenew.setId(banQiang2.getId());
                banRenew.setStatus(banQiang2.getStatus());
                String tname2 = sname(banQiang2.getName());
                banRenew.setEqid(tname2);
                banRenew.setContent(banQiang2.getContent());
                banRenewMapper.updateById(banRenew);
            }
            BanQiang banQiang3 = ban.getBanQiang3();
            if (banQiang3 != null) {
                BanRenew banRenew = new BanRenew();
                banRenew.setBanid(ban.getId());
                banRenew.setId(banQiang3.getId());
                banRenew.setStatus(banQiang3.getStatus());
                String tname3 = sname(banQiang3.getName());
                banRenew.setEqid(tname3);
                banRenew.setContent(banQiang3.getContent());
                banRenewMapper.updateById(banRenew);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PQCG, AppHttpCodeEnum.PQCG.getErrorMessage());

    }

    public String sname(List<String> s) {
        String sname = "";
        int i = 0;
        for (String s1 : s) {
            if (i == 0) {
                sname += s1;
            } else {
                sname += "," + s1;
            }
            i++;
        }
        return sname;
    }

    @Override
    public ResponseResult pageList(BanPage banPage) {
        //参数为空
        if (banPage == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        banPage.checkParam();
        PageHelper.startPage(banPage.getPage(), 20);
        ArrayList<Ban> detaile = sheGenMapper.sheGenpahe(banPage);
        for (Ban ban : detaile) {
            List<BanRenew> banRenews = banRenewMapper.selectList(Wrappers.<BanRenew>lambdaQuery().eq(BanRenew::getBanid, ban.getId()));
            String she = "";
            for (BanRenew banRenew : banRenews) {
                if (banRenew.getStatus() != 3) {
                    String eqid = banRenew.getEqid();
                    if (StringUtils.isNotBlank(eqid)) {
                        if (ban.getId().equals(banRenew.getBanid())) {
                            String[] split = banRenew.getEqid().split(",");
                            for (String s : split) {
                                she += s + ";";
                            }
                        }
                    }
                }
            }
            ban.setEqid(she);
        }
        PageInfo<Ban> pageInfo = new PageInfo<>(detaile);
        PageResponseResult responseResult = new PageResponseResult(banPage.getPage(), pageInfo.getSize(), (int) pageInfo.getTotal());
        responseResult.setData(pageInfo.getList());
        return responseResult;
    }

    @Override
    public ResponseResult findById(Integer id) {
        BanSave coursewareSave = new BanSave();
        try {
            Ban ban = sheGenMapper.selectById(id);
            BanPian pian = new BanPian();
            pian.setIname(ban.getMd() + ".apk");
            pian.setCapacity(ban.getCapacity());
            pian.setCreateDate(ban.getCreateDate());
            pian.setMd(ban.getMd());
            pian.setImg(ban.getImg());
            coursewareSave.setBanPian(pian);
            List<BanRenew> banRenews = banRenewMapper.selectList(Wrappers.<BanRenew>lambdaQuery().eq(BanRenew::getBanid, id));
            for (BanRenew banRenew : banRenews) {
                if (banRenew.getStatus() == 1) {
                    BanQiang banRenew1 = new BanQiang();
                    banRenew1.setId(banRenew.getId());
                    banRenew1.setContent(banRenew.getContent());
                    banRenew1.setStatus(1);
                    String eqid = banRenew.getEqid();
                    if (StringUtils.isNotBlank(eqid)) {
                        String[] split = eqid.split(",");
                        List<String> strings = Arrays.asList(split);
                        banRenew1.setName(strings);
                    }
                    coursewareSave.setBanQiang1(banRenew1);
                } else if (banRenew.getStatus() == 2) {
                    BanQiang banRenew1 = new BanQiang();
                    banRenew1.setId(banRenew.getId());
                    banRenew1.setContent(banRenew.getContent());
                    banRenew1.setStatus(2);
                    String eqid = banRenew.getEqid();
                    if (StringUtils.isNotBlank(eqid)) {
                        String[] split = eqid.split(",");
                        List<String> strings = Arrays.asList(split);
                        banRenew1.setName(strings);
                    }
                    coursewareSave.setBanQiang2(banRenew1);
                } else if (banRenew.getStatus() == 3) {
                    BanQiang banRenew1 = new BanQiang();
                    banRenew1.setId(banRenew.getId());
                    banRenew1.setContent(banRenew.getContent());
                    banRenew1.setStatus(3);
                    String eqid = banRenew.getEqid();
                    if (StringUtils.isNotBlank(eqid)) {
                        String[] split = eqid.split(",");
                        List<String> strings = Arrays.asList(split);
                        banRenew1.setName(strings);
                    }
                    coursewareSave.setBanQiang3(banRenew1);
                }
            }
            BeanUtils.copyProperties(ban, coursewareSave);
            return ResponseResult.success(coursewareSave);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, AppHttpCodeEnum.SERVER_ERROR.getErrorMessage());
    }


    @Override
    public ResponseResult xuan(Integer id) {

        List<Ban> bans = sheGenMapper.selectList(null);
        Ban ban1 = sheGenMapper.selectById(id);

        List<BanRenew> banRenews = banRenewMapper.selectList(Wrappers.<BanRenew>lambdaQuery().eq(BanRenew::getBanid, id));
        ArrayList<String> list = new ArrayList<>();

        if (id == null) {
            for (Ban ban : bans) {
                list.add(ban.getName());

            }
            return ResponseResult.okResult(list);
        }
        ArrayList<String> list1 = new ArrayList<>();
        for (BanRenew banRenew : banRenews) {
            if (StringUtils.isNotBlank(banRenew.getEqid())) {
                String[] split = banRenew.getEqid().split(",");
                for (String s : split) {
                    list1.add(s);
                }
            }
        }


        if (list1.size() < 1) {
            return ResponseResult.okResult(list);
        }
        //版本

        for (Ban ban : bans) {
            if (!ban1.getName().equals(ban.getName())) {
                list.add(ban.getName());
            }
        }

//        ArrayList<String> list3 = new ArrayList<>();
        if (list1.size() >= 1) {
            for (int i = 0; i < list1.size(); i++) {
                for (int j = 0; j < list.size(); j++) {
                    if (list1.get(i).equals(list.get(j))) {
                        list.remove(j);
                    }
                }
            }
        }
        return ResponseResult.okResult(list);
    }

}
